/*
 * > /*
>  * INSANE - Interactive Structural Analysis Environment
>  *
>  * Copyright (C) 2003-2004
>  * Universidade Federal de Minas Gerais
>  * Escola de Engenharia
>  * Departamento de Engenharia de Estruturas
>  *
>  * Author's email :     insane@dees.ufmg.br
>  * Author's Website :   http://www.dees.ufmg.br/insane
>  *
>  * This program is free software; you can redistribute it and/or
>  * modify it under the terms of the GNU General Public License
>  * as published by the Free Software Foundation; either version 2
>  * of the License, or any later version.
>  *
>  * This program is distributed in the hope that it will be useful,
>  * but WITHOUT ANY WARRANTY; without even the implied warranty of
>  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
>  * GNU General Public License for more details.
>  *
>  * You should have received a copy of the GNU General Public License
>  * along with this program; if not, write to the Free Software
>  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-
> 1307, USA.
 *
 */

package br.ufmg.dees.insane.ui.rich.postp.postp_view.dialog;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import br.ufmg.dees.insane.commons.view.ViewState;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.InterfacePostp;
import br.ufmg.dees.insane.ui.rich.postp.postp_view.command.LastZoomCommand;

/**
 * A class representing creation dialog on screen.<br>
 * @author Penna, Samuel & Pitangueira, Roque
 * @since september 2004
 */
public final class GridSettingsDialog extends SimpleDialog {

    /**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InterfacePostp parent;
    private JPanel gridPane = new JPanel();
    
	//private DecimalField xField;
    //private DecimalField yField;
    //private DecimalField widthField;
    //private DecimalField heightField;
    private DecimalField gridwidthField;
    private DecimalField gridheightField; 
    private DecimalField lineWidthDecimalField;
    
    //private JComboBox lineTypeCombo;
    
	private JTextField gridColorField = new JTextField();
    private JButton chooseGridColor = new JButton("Choose");
	
	private java.awt.geom.Rectangle2D.Double limits = new java.awt.geom.Rectangle2D.Double();
    //private JColorChooser colorChooser = new JColorChooser();
    private NumberFormat numberFormat;
    private ViewState state;
    //private String[]lineTypeStr;
	
//*****************************************************************************
	
    /**
     * Constructor method, calling super with the especific title.
     *
     * @param parent The parent of the class.
     * @see gui.Interface
     */
    public GridSettingsDialog(InterfacePostp parent, ViewState s) {
        this.parent = parent;
        state = s;
		
        setUpFormat();
		
        //Sets fields according to model
        gridheightField  = new DecimalField(state.getGridYInc(), 10, numberFormat);
        gridwidthField  = new DecimalField(state.getGridXInc(), 10, numberFormat);
        
		 // Sets up the grid panel
        JPanel gPane = new JPanel();
        gPane.setLayout(new BoxLayout(gPane, BoxLayout.Y_AXIS));
        gPane.add(gridPane);
        gridPane.setLayout(new GridLayout(5,2,2,2));
        gridPane.setMaximumSize(new Dimension(500,120));
        gridPane.setBorder(BorderFactory.createCompoundBorder(
        BorderFactory.createTitledBorder("Grid settings"),
        BorderFactory.createEmptyBorder(2,2,2,2)));
        gridPane.add(new JLabel(" Width Space:"));
        gridPane.add(new JLabel(" Heigt Space:"));
        gridPane.add(gridwidthField);
        gridPane.add(gridheightField);
        gridPane.add(new JLabel("Line Width:"));
        lineWidthDecimalField = new DecimalField(state.gridLineWidth,10,numberFormat);
        lineWidthDecimalField.setMaximumSize(new Dimension(75,25));
        lineWidthDecimalField.setPreferredSize(new Dimension(75,25));
        lineWidthDecimalField.setMinimumSize(new Dimension(75,25));
        lineWidthDecimalField.setSize(new Dimension(75,25));  
        lineWidthDecimalField.setEditable(true);
        gridPane.add(lineWidthDecimalField);
        gridPane.add(new JLabel(" Color:"));
        gridPane.add(new JLabel());
        gridColorField.setEditable(false);
        gridColorField.setBackground(state.gridColor);
        gridPane.add(gridColorField);
        gridPane.add(chooseGridColor);
        chooseGridColor.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                gridColorActionPerformed(e);
            }
        } );
		
		setPane(gridPane);
		
    }
	
//*****************************************************************************
    
    public int showDialog() {
        return super.showDialog(parent, "Grid Settings");
    }
	
//*****************************************************************************
	
    /**
     * This method is executed on OK button click.
     *
     */
    public void approvedPerform() {
        try {
            state.setGridIncrements(gridwidthField.getValue(), gridheightField.getValue());
            state.gridLineWidth = lineWidthDecimalField.getValue();
            state.gridColor = gridColorField.getBackground();
            
			state.setLimits(limits);
            
			//parent.setModel(((XYPlotController)parent.getView().getController()).getModel());
            this.setVisible(false);
			
			LastZoomCommand cmd = new LastZoomCommand(parent);
			cmd.execute();
			
        } catch (Exception err) {
            Toolkit.getDefaultToolkit().beep();
            this.setVisible(false);
        }
    }
    
//*****************************************************************************
	
    private void gridColorActionPerformed(ActionEvent e) {
        gridColorField.setBackground(JColorChooser.showDialog(this, "Choose Grid Color", gridColorField.getBackground()));
    }
    
//*****************************************************************************
	
    /**
     * Sets the number formater
     */
    private void setUpFormat() {
        numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
        numberFormat.setMaximumFractionDigits(3);
    }
	
//*****************************************************************************
}
